import axios from 'axios'
import vuex from 'vuex'
import {Loading, Message} from "element-ui";
let loadings;
// 该项目所有请求均为 get请求
export function request(url, params) {
  // 请求超过30秒则判定为超时
  const instance = axios.create({
    baseURL: 'http://120.77.44.219:3000',
    timeout: 30000,
    withCredentials: true,
  });

  // axios拦截器
  // 请求拦截
  instance.interceptors.request.use(config => {
    // console.log('请求拦截器');
    //  加载动画
    let options = {
      lock: true,
      text: '正在拼命加载中',
      spinner: 'el-icon-loading',
      fullscreen: true,
      background: 'rgba(0, 0, 0, 0.7)'
    };
    loadings = Loading.service(options)
    return config
  }, err => {
    loadings.close();
    Message.error(err)

  });

  // 响应拦截
  instance.interceptors.response.use(config => {
    loadings.close();
    return config;
  }, err => {
    loadings.close();
    Message.error(err.response.data.msg)
    // console.log([err]);
    // if (err.response.data.msg === '需要登录') {
    //     // cookie过期 退出登录
    //     // console.log(window.localStorage.getItem("userInfo"));
    //     // window.localStorage.setItem("userInfo", "");
    //     // 刷新页面
    //     // history.go(0)
    //     // 修改当前的登录状态
    //     // vuex.state.isLogin = false;
    //   console.log(err.response.data.msg)
    // } else {
    //     console.log(err.response.data.msg);
    // }
  });

  instance.defaults.withCredentials = true;

  if (params) {
    params = {
      params: params
    }

    return instance.get(url, params)
  } else {
    return instance.get(url)
  }

}
